package com.qeeyou.apps.android.craftsman.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.activity.login.ForgetPasswordActivity;
import com.qeeyou.apps.android.craftsman.activity.login.LoginActivity;
import com.qeeyou.apps.android.craftsman.event.LoginEvent;
import com.qeeyou.apps.android.craftsman.event.LogoutEvent;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.po.LoginInfo;
import com.qeeyou.apps.android.craftsman.service.SystemContext;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SettingActivity extends Activity {

    private ImageView btnBack;
    private LinearLayout btnClearCache;
    private LinearLayout btnMessageNotify;
    private LinearLayout btnModifyPassword;
    private Button btnLogout;
    private CustomLoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        setView();

        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4Setting);
        btnClearCache = (LinearLayout) this.findViewById(R.id.btnClearCache4Setting);
        btnMessageNotify = (LinearLayout) this.findViewById(R.id.btnMessageNofity4Setting);
        btnModifyPassword = (LinearLayout) this.findViewById(R.id.btnModifyPassword4Setting);
        btnLogout = (Button) this.findViewById(R.id.btnLogout4Setting);
    }

    private void setEvent() {
        btnBack.setOnClickListener(onBackClickListener);

        btnModifyPassword.setOnClickListener(onModifyPasswordClickListener);

        btnLogout.setOnClickListener(onLogoutClickListener);
    }

    //退出登录按钮
    private View.OnClickListener onLogoutClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String token = SystemContext.getInstance().getToken();
            if (token != null) {
                //TODO -- 退出登录
                new UserLogoutAction().execute();
            } else {
                Toast.makeText(SettingActivity.this, "用户未登录", Toast.LENGTH_SHORT).show();
            }
        }
    };

    //返回按钮点击事件
    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SettingActivity.this.finish();
        }
    };

    //修改密码
    private View.OnClickListener onModifyPasswordClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SettingActivity.this, ForgetPasswordActivity.class);
            SettingActivity.this.startActivity(intent);
        }
    };

    //用户退出登录
    public class UserLogoutAction extends AsyncTask<Void, Void, CommonInfo> {

        public UserLogoutAction() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(SettingActivity.this);
            }
            mLoadingDialog.setMessage("正在退出登录...");
            mLoadingDialog.show();
        }

        @Override
        protected void onPostExecute(CommonInfo commonInfo) {
            super.onPostExecute(commonInfo);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (commonInfo != null && commonInfo.getStatus().equalsIgnoreCase("1")) {
                Toast.makeText(SettingActivity.this, "退出登录成功", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SystemContext.getInstance().setToken(null);
                        SystemContext.getInstance().setUserId(null);
                        SystemContext.getInstance().setUsername(null);
                        EventBus.getDefault().post(new LogoutEvent());
                        SettingActivity.this.finish();
                    }
                }, 500);
            } else {
                SystemContext.getInstance().setToken(null);
                SystemContext.getInstance().setUserId(null);
                SystemContext.getInstance().setUsername(null);
                Toast.makeText(SettingActivity.this, "退出登录失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected CommonInfo doInBackground(Void... voids) {
            Map<String, String> params = new HashMap<String, String>();
            CommonInfo resultInfo = UserService.getInstance().userLogout(params);
            return resultInfo;
        }
    }
}
